<template>
  <k-form-modal width="480px" title="导出数据" label-width="0" v-bind="formModalState" v-on="formModalEvent">
    <el-form-item>
      <el-radio-group v-model="state.model.type">
        <el-radio v-for="(o, i) in typeOptions" :key="i" :label="o.value" :disabled="o.disabled">{{ o.label }}</el-radio>
      </el-radio-group>
    </el-form-item>
    <el-form-item v-if="state.model.type === 1" prop="_date">
      <el-date-picker v-model="state.model._date" type="daterange" value-format="YYYYMMDD" placeholder="请选择日期" :shortcuts="dateShortcut" style="flex: none; width: 260px;" />
    </el-form-item>
  </k-form-modal>
</template>

<script setup lang="ts">
import { useFormModal } from '@crud/index';
import { lecturerListExport, lecturerListExport2 } from '@/api/crm-kgj/download';
import { useDateRange } from '@/utils';
import { AuthCode, hasPermission } from '@/modules/auth';
defineOptions({
  name: 'KgjLecturerListExportModal'
});

const typeOptions = [
  { label: '导出全部', value: 0 },
  { label: '按用户绑定时间', value: 1, disabled: !hasPermission(AuthCode.AGENCY_LECTURER_BIND_TIME_EXPORT, false) }
];
const dateShortcut = useDateRange(
  [
    [7, 1],
    [30, 1],
    [90, 1]
  ],
  ['substract', 'substract']
);

const { state, formModalState, formModalEvent, ...methods } = useFormModal({
  model: {
    type: 0, // 导出类型
    _date: [],
    from_dt: undefined,
    to_dt: undefined,
    follower: undefined
  },
  rules: {
    _date: [{ required: true, message: '请选择完整的日期时间' }]
  },
  onShow: (mode: number, data: any) => {
    state.model.type = 0;
    state.model.from_dt = undefined;
    state.model.to_dt = undefined;
    state.model.follower = data?.keyword;
    if (data?._dateRange?.length) {
      state.model.register_from_dt = data?._dateRange[0];
      state.model.register_to_dt = data?._dateRange[1];
    }
  },
  getParams: ({ _date = [], ...params }) => {
    if (state.model.type === 1) {
      const [from_dt, to_dt] = _date;
      if (from_dt) params.from_dt = from_dt;
      if (to_dt) params.to_dt = to_dt;
    }
    return params;
  },
  onSubmit: (params: any) => {
    const fn = params.type === 0 ? lecturerListExport : lecturerListExport2;
    fn(params);
  }
});
defineExpose({
  show: methods.show
});
</script>
